home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18290 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: druid.borland.com!usenet
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: templates, arguments and pointers
  5. Date: 19 Apr 1996 20:51:53 GMT
  6. Organization: Borland International
  7. Message-ID: <4l8ud9$esj@druid.borland.com>
  8. References: <31763ECE.352C@afrodite.kih.no>
  9. NNTP-Posting-Host: pbecker.borland.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <31763ECE.352C@afrodite.kih.no>, paulken4@afrodite.kih.no says...
  15. >
  16. >Here is a problem we have:
  17. >
  18. >template <class D>
  19. >        class Matrix : public Grid<D>
  20. >        {
  21. >        public:
  22. >                Matrix();
  23. >                Matrix(size_t rows, size_t cols);
  24. >                etc.
  25. >        }
  26. >
  27. >class tobject
  28. >{
  29. >public:
  30. >        tobject();
  31. >        ~tobject();
  32. >private:
  33. >        Matrix<int> *pointer;
  34. >}
  35. >
  36. >tobject::tobject()
  37. >{
  38. >        int i;
  39. >        pointer = new Matrix<int>(23,23); // is this construct valid?
  40. >}
  41. >
  42. >My problem is in the constructor of tobject. The Matrix class
  43. >(are from a book, and all examples pertaining to it, just
  44. >instantiates the objects with those two arguments always, never
  45. >dealing with pointers to the Matrix class) demands (as it is
  46. >now) the two arguments, saying how wide and how high it should
  47. >be.
  48. >
  49. >The standard way to fill in a Matrix object is this:
  50. >
  51. >Matrix<int> A(2,2);
  52. >
  53. >A(0,0) = 1;
  54. >A(0,1) = 2;
  55. >
  56. >How would this work with pointers:
  57. >
  58. >*this->pointer(0,0) = 1;
  59. >*this->pointer(0,1) = 2;
  60. >
  61.  
  62. Take two steps backward. You're too close to the code, and missing the 
  63. overview. pointer is, indeed, a pointer. You need to dereference it to be able 
  64. to talk about the object that it points to. So you need to say *pointer. Once 
  65. you've done that, you can invoke its members, but you have to deal with 
  66. operator precedence. That is, *pointer(0,0) says to apply the function call 
  67. operator to pointer and dereference the result. Of course, that doesn't make 
  68. sense. So tell the compiler to apply the function call operator to *pointer. 
  69. Like this:  (*pointer)(0,0). That's all you need.
  70.     -- Pete
  71.  
  72.